package com.first.controller;

import com.first.dao.RoleDao;
import com.first.entity.Role;
import com.first.util.LayTab;
import com.first.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleDao roleDao;

    /**
     * 获取全部角色信息展示到页面
     * @return
     */
    @RequestMapping("getAllRole")
    public LayTab getAllRole(Role role){
        List<Map> list = roleDao.queryAll(role);
        int count = roleDao.queryAllCount();
        if(list!=null){
            return new LayTab(count,list);
        }
        return new LayTab();
    }

    /**
     * 新增和修改角色的表单提交方法
     * @param role
     * @return
     */
    @RequestMapping("/editRole")
    public Result editRole(Role role){
        Result result = new Result();
        //判断，如果传来的数据有rid，证明是修改，否则，就是新增

        if(role.getRid()==null || "".equals(role.getRid())){
            int i = roleDao.addRole(role);
            if(i==-2){
                result.setCode(0);
                result.setMsg("角色已存在");
            }else if(i>0){
                result.setCode(200);
                result.setMsg("添加成功");
            }else {
                result.setCode(0);
                result.setMsg("添加失败");
            }
        }else{
            int i = roleDao.updateRole(role);
            if(i>0){
                result.setCode(200);
                result.setMsg("修改成功");
            }else {
                result.setCode(0);
                result.setMsg("修改失败");
            }
        }
        return result;
    }

    /**
     * 删除一个角色
     * @param rid
     * @return
     */
    @RequestMapping("delRole")
    public Result delRole(Integer rid){
        Result result = new Result();
        int i = roleDao.delRode(rid);
        if(i>0){
            result.setCode(200);
            result.setMsg("删除成功");
        }else {
            result.setCode(300);
            result.setMsg("删除失败");
        }
        return result;
    }
}
